package com.spring.courseapi.web.controller;



import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.fasterxml.jackson.databind.util.JSONPObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mangofactory.swagger.annotations.ApiIgnore;
import com.spera.genutil.TokenUtil;
import com.spring.comment.web.service.CommentService;
import com.spring.courseapi.web.service.CourseApiService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;
@Controller
@RequestMapping(value = "/courseApi")
@ApiIgnore
public class CourseApiController {
	
	@Resource
	private CourseApiService courseApiService;
	@Resource
	private CommentService commentService;	
	@Resource
	private JedisCache jedisCache;
	
//String token,String card_id,String page_size,
//	String page_index,String skip,String rows,String callback
	//课程科目类型集合
	@RequestMapping(value = "/getCourseTypeList")
	@ResponseBody
    public Object getCourseTypeList(String callback) {
   
        Map<String, Object> m = new HashMap<String, Object>();
		try {

			m  = courseApiService.getCourseTypeList();
					
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}
	
    	if(ApplicationUtils.isNotNull(callback)){
        	return new JSONPObject(callback, m);
        }else{
        	return m;
        }

    }

	
	//课程查询
		@RequestMapping(value = "/getCourseList")
		@ResponseBody
	    public Object getCourseList(String course_type_id,String course_name,String course_user_type_code,
	    		String course_base_id,String callback) {
	   
	        Map<String, Object> m = new HashMap<String, Object>();
			try {

				m  = courseApiService.getCourseList(course_type_id, course_name, course_user_type_code, course_base_id);
						
			} catch (Exception e) {
				m.put(ApplicationUtils.STATUS, "0");
				m.put(ApplicationUtils.MSG, "系统异常");
				e.printStackTrace();

			}

	    	
	    	if(ApplicationUtils.isNotNull(callback)){
	        	return new JSONPObject(callback, m);
	        }else{
	        	return m;
	        }
	        
	        
	    }
	
				/**
				 * 课程表查询
				 * @param course_open_id
				 * @param start_time
				 * @param end_time
				 * @param kids_park_id
				 * @param page_size
				 * @param page_index
				 * @param skip
				 * @param rows
				 * @param callback
				 * @return
				 */
				@RequestMapping(value = "/getCourseOpenDetailList")
				@ResponseBody
			    public Object getCourseOpenDetailList(String course_id,String course_base_id,String course_open_detail_id,String start_time,String end_time,
						String kids_park_id,String page_size,String page_index,String skip,String rows,String callback) {
			   
			            Map<String, Object> m = new HashMap<String, Object>();
					try {

						m  = courseApiService.getCourseOpenDetailList(course_id,course_base_id,course_open_detail_id, start_time, end_time, kids_park_id, page_size, page_index, skip, rows);					
				
					} catch (Exception e) {
						m.put(ApplicationUtils.STATUS, "0");
						m.put(ApplicationUtils.MSG, "系统异常");
						e.printStackTrace();
					}			    	
			    	if(ApplicationUtils.isNotNull(callback)){
			        	return new JSONPObject(callback, m);
			        }else{
			        	return m;
			        }			        
			        
			    }
				
				
				/**
				 * 得到个人
				 * @param course_id
				 * @param course_base_id
				 * @param course_open_detail_id
				 * @param start_time
				 * @param end_time
				 * @param kids_park_id
				 * @param page_size
				 * @param page_index
				 * @param skip
				 * @param rows
				 * @param callback
				 * @return
				 */
				@RequestMapping(value = "/getMyCourseOpenDetailList")
				@ResponseBody
			    public Object getMyCourseOpenDetailList(String token,String course_id,String c_user_id,String course_base_id,String course_open_detail_id,String start_time,String end_time,
						String kids_park_id,String page_size,String page_index,String skip,String rows,String callback) {
			   
			            Map<String, Object> m = new HashMap<String, Object>();
					try {

						m  = courseApiService.getMyCourseOpenDetailList(token,course_id,c_user_id,course_base_id,course_open_detail_id, start_time, end_time, kids_park_id, page_size, page_index, skip, rows);					
				
					} catch (Exception e) {
						m.put(ApplicationUtils.STATUS, "0");
						m.put(ApplicationUtils.MSG, "系统异常");
						e.printStackTrace();
					}			    	
			    	if(ApplicationUtils.isNotNull(callback)){
			        	return new JSONPObject(callback, m);
			        }else{
			        	return m;
			        }			        
			        
			    }
				
				/*
				 * 预约课程
				 */
				@RequestMapping(value = "/appointCourse")
				@ResponseBody
			    public Object appointCourse(String token,String course_open_detail_id,String c_user_id,
						String course_user_type_code,String real_name,String phone,String callback) {
			   
			        Map<String, Object> m = new HashMap<String, Object>();
					try {

						m  = courseApiService.appointCourse(token, course_open_detail_id, c_user_id, course_user_type_code,real_name,phone);
						
					} catch (Exception e) {
						m.put(ApplicationUtils.STATUS, "0");
						m.put(ApplicationUtils.MSG, "系统异常");
						e.printStackTrace();

					}

			    	
			    	if(ApplicationUtils.isNotNull(callback)){
			        	return new JSONPObject(callback, m);
			        }else{
			        	return m;
			        }
			        
			        
			    }	
				
				/*
				 * 获取用户预约的课程接口/查看签到记录
				 */
				@RequestMapping(value = "/getCourseAppointmentList")
				@ResponseBody
			    public Object getCourseAppointmentList(String token,String c_user_id,
						String course_user_type_code,String isattended,String start_time,String end_time
						,String page_size,String page_index,String skip,String rows,String callback) {
			   
			        Map<String, Object> m = new HashMap<String, Object>();
					try {

						m  = courseApiService.getCourseAppointmentList(token, c_user_id, course_user_type_code, isattended, start_time, end_time, page_size, page_index, skip, rows);
						
					} catch (Exception e) {
						m.put(ApplicationUtils.STATUS, "0");
						m.put(ApplicationUtils.MSG, "系统异常");
						e.printStackTrace();

					}

			    	
			    	if(ApplicationUtils.isNotNull(callback)){
			        	return new JSONPObject(callback, m);
			        }else{
			        	return m;
			        }
			        
			        
			    }	
				
				
	/*
	 * 取消预约课程
	 */
	@RequestMapping(value = "/cancelCourse")
	@ResponseBody
	public Object cancelCourse(String token, String course_appointmet_id,
			String callback) {

		Map<String, Object> m = new HashMap<String, Object>();
		try {

			m = courseApiService.cancelCourse(token, course_appointmet_id);

		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}

		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}

	}
	
	/*
	 * 家长评论课程接口
	 */
	@RequestMapping(value = "/parentCommentCourseOld")
	@ResponseBody
	public Object parentCommentCourse(String token, String course_open_detail_id,
			String comment,String c_user_name,String callback) {

		Map<String, Object> m = new HashMap<String, Object>();
		try {
			
			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
			if (t != null) {
				return t;
			}
			
//			String c_user_id = jedisCache.lindex(token, 0);
//			String user_name = jedisCache.lindex(token, 3);
			m = courseApiService.parentCommentCourse(token, course_open_detail_id, comment,c_user_name);
			//m = commentService.insertCommentService(c_user_id, "1", user_name, course_open_detail_id, "1001", "2", comment);
			
			
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}

		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}

	}
	
	/*
	 * 家长评论课程接口
	 */
	@RequestMapping(value = "/parentCommentCourse")
	@ResponseBody
	public Object parentCommentCourseNew(String token, String course_open_detail_id,
			String comment,String c_user_name,String callback) {

		Map<String, Object> m = new HashMap<String, Object>();
		try {
			
			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
			if (t != null) {
				return t;
			}
			
			String c_user_id = jedisCache.hget(token, TokenUtil.USER_ID);
			String user_name = jedisCache.hget(token, TokenUtil.USER_NAME);
			//m = courseApiService.parentCommentCourse(token, course_open_detail_id, comment,c_user_name);
			m = commentService.insertCommentService(c_user_id, "1", user_name, course_open_detail_id, "1002", "2", comment);
			
			
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}

		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}

	}
	

	
	//获取课程视频照片辅导员反馈
	@RequestMapping(value = "/getCourseCommentList")
	@ResponseBody
	public Object getCourseCommentList(String token, String course_open_detailID,String baby_id,String callback) {

		Map<String, Object> m = new HashMap<String, Object>();
		try {
			
			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
			if (t != null) {
				return t;
			}
			
//			String c_user_id = jedisCache.lindex(token, 0);
//			String user_name = jedisCache.lindex(token, 3);
			//m = courseApiService.parentCommentCourse(token, course_open_detail_id, comment,c_user_name);
			m =courseApiService.getCourseCommentList(token,course_open_detailID,baby_id);
			
			
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}

		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}

	}
	
	

			
	//获取课程视频照片辅导员反馈
	@RequestMapping(value = "/getCourseRateList")
	@ResponseBody
	public Object getCourseRateList(String token, String course_open_detailID,String pageSize,String pageIndex,String skip,String rows,String callback) {

		Map<String, Object> m = new HashMap<String, Object>();
		try {
			
			Map<String, Object> t = TokenUtil.checkToken(token, jedisCache);
			if (t != null) {
				return t;
			}
			
//			String c_user_id = jedisCache.lindex(token, 0);
//			String user_name = jedisCache.lindex(token, 3);
			//m = courseApiService.parentCommentCourse(token, course_open_detail_id, comment,c_user_name);
			m =courseApiService.getCourseRateList(token, course_open_detailID, pageSize, pageIndex, skip, rows);
			
			
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}

		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}

	}
		
	
	//获取课程
	@RequestMapping(value = "/getCourseByID")
	@ResponseBody
	public Object getCourseByID(String courseID,String callback) {

		Map<String, Object> m = new HashMap<String, Object>();
		try {

			m =courseApiService.getCourseByID(courseID);
			
			
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}

		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}

	}
	
	
	
	//获取教师
	@RequestMapping(value = "/getTeacherByID")
	@ResponseBody
	public Object getTeacherByID(String t_user_id,String callback) {

		Map<String, Object> m = new HashMap<String, Object>();
		try {

			m =courseApiService.getTeacherByID(t_user_id);
			
			
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}

		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}

	}
	
	
	//获取教师
	@RequestMapping(value = "/getTodayCourse")
	@ResponseBody
	public Object getToDayCourse(String kids_parkID,String callback) {

		Map<String, Object> m = new HashMap<String, Object>();
		try {

			m =courseApiService.getToDayCourse(kids_parkID);
			
			
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}

		if (ApplicationUtils.isNotNull(callback)) {
			return new JSONPObject(callback, m);
		} else {
			return m;
		}

	}
}

